from mod_thing import Thing
import random,mod_line,mod_actor,mod_entity,mod_gui,mod_ghost
import mod_idgen
import mod_verbs

class ItemPrototype:
    def __init__(self,glyph,grabbable,visible=True,stopsBeforeWall=True,eqLoc=None):
        self.glyph = glyph
        self.verbs = []
        self.grabbable = grabbable
        if grabbable:
            self.verbs.append(mod_verbs.staticDropVerb)
            self.verbs.append(mod_verbs.staticThrowVerb)
            self.verbs.append(mod_verbs.staticPickUpVerb)
        self.steppedOnEffect = None
        self.steppedOffEffect = None
        self.startTurnOnEffect = None
        self.impactEffect = None
        self.stopsBeforeWall = stopsBeforeWall
        self.visible = visible
        self.eqLoc = eqLoc
        self.resistanceMap = {}
        self.maxHP = 2 # TEMPORARY
    
    def instantiate(self):
        return Item(self)


class Item(Thing):
    def __init__(self,prototype):
        Thing.__init__(self,prototype)
        self.equipped = False
    
    # newLocation is either an actor or a (level,coords) tuple
    def setLocation(self,newLocation):
        oldLocation = self.location
        
        oldLevel,oldCoords = None,None
        newLevel,newCoords = None,None
        
        if oldLocation:
            if isinstance(oldLocation,mod_entity.Entity):
                oldLocation.items.remove(self) 
            else:
                oldLevel,oldCoords = oldLocation
                oldLevel.itemMap[oldCoords].remove(self)
                if not oldLevel.itemMap[oldCoords]: #If there are no more items left...
                    del oldLevel.itemMap[oldCoords] #...delete the list.
        if newLocation:
            if isinstance(newLocation,mod_entity.Entity):
                newLocation.items.append(self)
            else:
                newLevel,newCoords = newLocation
                if not newCoords in newLevel.itemMap:
                    newLevel.itemMap[newCoords] = []
                newLevel.itemMap[newCoords].append(self)
                
        self.location = newLocation
        
        if oldLevel:
            otherLocation = (oldLevel.number,oldCoords)
            oldLevel.propagateGhost(self.generateGhost(),otherLocation)
        else:
            otherLocation = None
        if newLevel and oldLevel != newLevel:
            newLevel.propagateGhost(self.generateGhost(),otherLocation)
    
    def generateGhost(self):
        if (not self.location) or isinstance(self.location,mod_entity.Entity):
            ghostLocation = None
        else:
            level,coords = self.location
            ghostLocation = (level.number,coords)
        ghost = mod_ghost.ItemGhost(self.ID,self.prototype,ghostLocation)
        return ghost
    
    def die(self):
        if self.equipped:
            self.location.unequipItem(self) # Location must be an actor.
        self.setLocation(None)
    
    def __getattr__(self,name):
        return getattr(self.prototype,name)
